<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    body {
        margin: 0;
        padding: 30px;
    }
</style>
<!-- 本地导入 -->
<script src="./node_modules/axios/dist/axios.min.js"></script>

<body>
    <h1>修改用户</h1>
    <label for="uesername">用户名</label>
    <input type="text" id="username">
    <br>
    <label for="password">密码</label>
    <input type="password" id="password">
    <br>
    <button id="add">添加</button>

    <script>
        // // 获取location的search
        // console.log(location)
        // console.log(location.search)
        // console.log(location.search.replace('?',''))
        // console.log(location.search.replace('?','').split('=')[1])
        // 方式一
        // const id = location.search.replace('?', '').split('=')[1]

        // 方式二
        const id=new URLSearchParams(location.search).get('id')
        console.log(id)
        axios.get(`http://localhost:3000/users/${id}`).then(res=>{
            const user=res.data
            console.log(user)
            document.querySelector('#username').value=user.username
            document.querySelector('#password').value=user.password
        })






       // 获取提交按钮
        document.querySelector('button').addEventListener('click',function(){
            // 获取用户名和密码
            const username = document.querySelector('#username').value
            const password = document.querySelector('#password').value
            // 发送请求
            axios({
                url:`http://localhost:3000/users/${id}`,
                method:'put',
                data:{username:username,password:password}
            }).then(function (response) {
                // 请求成功后再跳转页面
                location.href = './index.html';}
            )

        })

    </script>
</body>

</html>